home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / WINSYS.PAK / WSYSCLS.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  273 lines

  1. //----------------------------------------------------------------------------
  2. // Borland WinSys Library
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.13  $
  6. //
  7. // Classes for window system structure and type encapsulation
  8. //----------------------------------------------------------------------------
  9. #if !defined(WINSYS_WINCLASS_H)
  10. #define WINSYS_WINCLASS_H
  11.  
  12. #if !defined(WINSYS_DEFS_H)
  13. # include <winsys/defs.h>
  14. #endif
  15. #if !defined(WINSYS_GEOMETRY_H)
  16. # include <winsys/geometry.h>     // TPoint, et. al.
  17. #endif
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace ClassLib {
  21. #endif
  22.  
  23. //
  24. // class TResId
  25. // ~~~~~ ~~~~~~
  26. // Resource Id class that can be constructed from a integer or string resource
  27. // identifier. Does not own or delete the string.
  28. //
  29. class TResId {
  30.   public:
  31.     // Constructors
  32.     //
  33.     TResId() : Num(0) {}
  34.     TResId(const _TCHAR far* resStr) : Str(resStr) {}
  35.     TResId(int resNum) : Num(unsigned(resNum)) {}
  36.  
  37.     // Type Conversion operators & information
  38.     //
  39.     operator _TCHAR far*() {return (_TCHAR far*)Str;}
  40.     bool IsString() const {return HiUint16(Num) != 0;}
  41.  
  42.   private:
  43.     union {
  44.       uint32          Num;  // Assumes that these 32bit fields will overlay
  45.       const _TCHAR far* Str;
  46.     };
  47.  
  48.   friend ipstream& _WSYSFUNC operator >>(ipstream& is, TResId& id);
  49.   friend opstream& _WSYSFUNC operator <<(opstream& os, const TResId& id);
  50.   friend ostream& _WSYSFUNC  operator <<(ostream& os, const TResId& id);
  51. };
  52.  
  53. #if defined(BI_NAMESPACE)
  54. }     // namespace ClassLib
  55. #endif
  56.  
  57. //
  58. // MSW only classes
  59. //
  60. #if defined(BI_PLAT_MSW)
  61.  
  62. # if !defined(WINSYS_WSYSINC_H)
  63. #   include <winsys/wsysinc.h>
  64. # endif
  65. # if !defined(CLASSLIB_BAGS_H)
  66. #   include <classlib/bags.h>
  67. # endif
  68.  
  69. #if defined(BI_NAMESPACE)
  70. namespace ClassLib {
  71. #endif
  72.  
  73. //
  74. // class TDropInfo
  75. // ~~~~~ ~~~~~~~~~
  76. // Encapsulation of a system drag-drop event object in an HDROP handle.
  77. //
  78. class TDropInfo {
  79.   public:
  80.     // Constructors
  81.     //
  82.     TDropInfo(HDROP handle) : Handle(handle) {}
  83.  
  84.     // Type Conversion operators
  85.     //
  86.     operator HDROP() {return Handle;}
  87.  
  88.     // Information access
  89.     //
  90.     uint DragQueryFile(uint index, _TCHAR far* name, uint nameLen)
  91.            {return ::DragQueryFile(Handle, index, name, nameLen);}
  92.     uint DragQueryFileCount() {return ::DragQueryFile(Handle, uint(-1), 0, 0);}
  93.     uint DragQueryFileNameLen(uint index)
  94.            {return ::DragQueryFile(Handle, index, 0, 0);}
  95.     bool DragQueryPoint(TPoint& point)
  96.            {return ::DragQueryPoint(Handle, &point);}
  97.     void DragFinish() {::DragFinish(Handle);}
  98.  
  99.   private:
  100.     HDROP  Handle;
  101. };
  102.  
  103. //
  104. // class TFileDrop
  105. // ~~~~~ ~~~~~~~~~
  106. // Encapsulation of information about a single dropped file, its name, where
  107. // it was dropped, and whether or not it was in the client area
  108. //
  109. class _WSYSCLASS TFileDroplet {
  110.   public:
  111.     TFileDroplet(const _TCHAR* fileName, TPoint& p, bool inClient);
  112.     TFileDroplet(TDropInfo& drop, int i);
  113.    ~TFileDroplet();
  114.  
  115.     operator ==(const TFileDroplet& other) const {return &other == this;}
  116.  
  117.     const _TCHAR* GetName() const {return FileName;}
  118.     TPoint      GetPoint() const {return Point;}
  119.     bool        GetInClientArea() const {return InClientArea;}
  120.  
  121.   private:
  122.     _TCHAR*   FileName;
  123.     TPoint  Point;
  124.     bool    InClientArea;
  125.  
  126.     // Hidden to prevent accidental copying or assignment
  127.     //
  128.     TFileDroplet(const TFileDroplet&);
  129.     TFileDroplet& operator =(const TFileDroplet&);
  130. };
  131.  
  132. typedef TIBagAsVector<TFileDroplet> TFileDropletList;
  133. typedef TIBagAsVectorIterator<TFileDroplet> TFileDropletListIter;
  134.  
  135. //
  136. // class TProcInstance
  137. // ~~~~~ ~~~~~~~~~~~~~
  138. // A ProcInstance object. This encapsulates the MakeProcInstance call, which is
  139. // really only needed in old Win3.X real mode. This exists now for Owl 2.x
  140. // compatibility only
  141. //
  142. class TProcInstance {
  143.   public:
  144. #if defined(BI_PLAT_WIN16) && defined(OWL_PROCINSTANCE)
  145.     TProcInstance(FARPROC p) {Instance = ::MakeProcInstance(p, _hInstance);}
  146.    ~TProcInstance() {::FreeProcInstance(Instance);}
  147. #else
  148.     TProcInstance(FARPROC p) {Instance = FARPROC(p);}
  149. #endif
  150.  
  151.     operator FARPROC() {return Instance;}
  152.  
  153.   private:
  154.     FARPROC Instance;
  155. };
  156.  
  157. //
  158. // TResource simplifies access to a resource by encapsulating
  159. // the find, load, lock and free steps for accessing a resource.
  160. //   - 'T' represents a structure which defines the binary layout of the
  161. //     resource.
  162. //   - 'resType' is a constant string that defines the resource type.
  163. //
  164. // For example,
  165. //
  166. //     typedef TResource<DLGTEMPLATE, RT_DIALOG> TDlgResource;
  167. //     TDlgResource dlgInfo(hInstance, IDD_ABOUTDLG);
  168. //     DLGTEMPLATE* pDlgTmpl = dlgInfo;
  169. //
  170. template <class T, const _TCHAR far* resType>
  171. class _WSYSCLASS TResource {
  172.   public:
  173.     TResource(HINSTANCE hModule, TResId resId);
  174. #if defined(BI_PLAT_WIN32)
  175.     TResource(HINSTANCE hModule, TResId resid, LANGID langid);
  176. #endif
  177.    ~TResource();
  178.     bool      IsOK() const;     // Confirms whether resource was found
  179.     operator  T*();             // Conversion operator to point to
  180.                                 // structure representing binary layout
  181.                                 // of the resource...
  182.   protected:
  183.     HGLOBAL   MemHandle;        // Handle of resource
  184.     T*        MemPtr;           // Pointer to locked resource
  185. };
  186.  
  187. //
  188. // Loads & locks the specified resource..
  189. //
  190. template <class T, const _TCHAR far* resType>
  191. TResource<T, resType>::TResource(HINSTANCE hModule, TResId resId)
  192. :
  193.   MemHandle(0),
  194.   MemPtr(0)
  195. {
  196.   HRSRC resHandle = FindResource(hModule, resId, resType);
  197.   if (resHandle) {
  198.     MemHandle = LoadResource(hModule, resHandle);
  199.     if (MemHandle)
  200.       MemPtr = (T*)LockResource(MemHandle);
  201.   }
  202. }
  203.  
  204. #if defined(BI_PLAT_WIN32)
  205. //
  206. // Loads & locks a resource of the type 'resType' from the module
  207. // (hModule). Accepts a LanguageID for localized resources.
  208. //
  209. template <class T, const _TCHAR far* resType>
  210. TResource<T, resType>::TResource(HINSTANCE hModule, TResId resId, LANGID lcid)
  211. :
  212.   MemHandle(0),
  213.   MemPtr(0)
  214. {
  215.   HRSRC resHandle = FindResourceEx(hModule, resId, resType, lcid);
  216.   if (resHandle) {
  217.     MemHandle = LoadResource(hModule, resHandle);
  218.     if (MemHandle)
  219.       MemPtr = (T*)LockResource(MemHandle);
  220.   }
  221. }
  222. #endif
  223.  
  224. //
  225. // Unlocks and frees the resource loaded earlier.
  226. // NOTE: Unlocking and freeing of resources is not necessary in WIN32.
  227. //
  228. template <class T, const _TCHAR far* resType>
  229. TResource<T, resType>::~TResource()
  230. {
  231.   // NOTE: UnlockResource and FreeResource are not necessary (albeit harmless)
  232.   // in 32-bit
  233.   //
  234. #if defined(BI_PLAT_WIN16)
  235.   if (MemHandle) {
  236.     UnlockResource(MemHandle);
  237.     FreeResource(MemHandle);
  238.   }
  239. #endif
  240.   MemHandle = 0;
  241.   MemPtr = 0;
  242. }
  243.  
  244. //
  245. // Returns true if resource was successfully loaded or false otherwise.
  246. //
  247. template <class T, const _TCHAR far* resType> bool
  248. TResource<T, resType>::IsOK() const
  249. {
  250.   return ((MemHandle != 0) && (MemPtr != 0));
  251. }
  252.  
  253. //
  254. // Returns a pointer to the locked resource.
  255. // NOTE: This operator eliminates the need to explicitly cast
  256. //       LPVOID to a structure representing the actual layout
  257. //       of individual resource types.
  258. //
  259. template <class T, const _TCHAR far* resType>
  260. TResource<T, resType>::operator T* ()
  261. {
  262.   PRECONDITION(IsOK());
  263.   return MemPtr;
  264. }
  265.  
  266. #if defined(BI_NAMESPACE)
  267. }     // namespace ClassLib
  268. #endif
  269.  
  270. #endif  // BI_PLAT_MSW
  271.  
  272. #endif  // WINSYS_WINCLASS_H
  273.